package com.easyadmin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.easyadmin.entity.enums.PageSize;
import com.easyadmin.entity.query.SysRole2MenuQuery;
import com.easyadmin.entity.po.SysRole2Menu;
import com.easyadmin.entity.vo.PaginationResult;
import com.easyadmin.entity.vo.SimplePage;
import com.easyadmin.exception.BusinessException;import org.springframework.dao.DuplicateKeyException;
import com.easyadmin.mappers.SysRole2MenuMapper;
import com.easyadmin.service.SysRole2MenuService;


/**
 * 
 * 角色对应的菜单权限表 业务接口实现
 * 
 */
@Service("sysRole2MenuService")
public class SysRole2MenuServiceImpl implements SysRole2MenuService {

	@Resource
	private SysRole2MenuMapper<SysRole2Menu,SysRole2MenuQuery> sysRole2MenuMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<SysRole2Menu> findListByParam(SysRole2MenuQuery param) {
		return this.sysRole2MenuMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(SysRole2MenuQuery param) {
		return this.sysRole2MenuMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResult<SysRole2Menu> findListByPage(SysRole2MenuQuery param) {
		int count = this.sysRole2MenuMapper.selectCount(param);
		int pageSize = param.getPageSize()==null?PageSize.SIZE15.getSize():param.getPageSize();
		int pageNo = 0;
		if (null != param.getPageNo()) {
			pageNo=param.getPageNo();
		}
		SimplePage page = new SimplePage(pageNo, count, pageSize);
		param.setSimplePage(page);
		List<SysRole2Menu> list = this.sysRole2MenuMapper.selectList(param);
		PaginationResult<SysRole2Menu> result = new PaginationResult<>(count,pageSize,pageNo,page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(SysRole2Menu bean) throws BusinessException{
		try {
			return this.sysRole2MenuMapper.insert(bean);
		} catch (DuplicateKeyException e) {
			throw new BusinessException("信息已经存在");
		}
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<SysRole2Menu> listBean){
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.sysRole2MenuMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<SysRole2Menu> listBean){
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.sysRole2MenuMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据RoleIdAndMenuId修改
	 */
	@Override
	public Integer updateByRoleIdAndMenuId(SysRole2Menu bean,Integer roleId,Integer menuId){
		return this.sysRole2MenuMapper.updateByRoleIdAndMenuId(bean,roleId,menuId);
	}


	/**
	 * 根据RoleIdAndMenuId删除
	 */
	@Override
	public Integer deleteByRoleIdAndMenuId(Integer roleId,Integer menuId){
		return this.sysRole2MenuMapper.deleteByRoleIdAndMenuId(roleId,menuId);
	}


	/**
	 * 根据RoleIdAndMenuId获取对象
	 */
	@Override
	public SysRole2Menu getSysRole2MenuByRoleIdAndMenuId(Integer roleId,Integer menuId){
		return this.sysRole2MenuMapper.selectByRoleIdAndMenuId(roleId,menuId);
	}

}